﻿/**
 * SlotDuino
 * 
 * Copyright (c) 2011/2012 Thiago Hummel Moreira, Ricardo Mizuuti, Rafael Mufato, Bruno Sakai, Andre Turina
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Linq;
using SlotDuino.DAL;
using SlotDuino.Util;
using SlotDuino.Sockets;

namespace SlotDuino.UI.Pages
{
    /// <summary>
    /// Interaction logic for Dashboard.xaml
    /// </summary>
    public partial class Dashboard : Page
    {
        private SlotDaemonClient SockectClient { get; set; }

        public Dashboard()
        {
            InitializeComponent();
        }

        private void NavigateTo(string page)
        {
            NavigationService.Navigate(new Uri(page, UriKind.Relative));
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //Avoid going back to login/first run
            NavigationService.RemoveBackEntry();

            //Get the instance of our application
            App application = (App)Application.Current;

            //ACL
            if (!application.LoggedUser.IsAdministrator)
            {
                btnOperators.IsEnabled = false;
                btnTrackConfig.IsEnabled = false;
                btnPreferences.IsEnabled = false;
            }

            //Keep an instance of the SlotDaemonClient
            SockectClient = application.Client;

            //Listen to client connection status changes
            SockectClient.ConnectionStatusChanged += new Sockets.ConnectionStatusChangedEventHandler(OnConnectionStatusChanged);
            SetConnected(SockectClient.ConnectionStatus);
        }

        private void btnPilots_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo("UI/Pages/PilotManager.xaml");
        }

        private void btnOperators_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo("UI/Pages/OperatorManager.xaml");
        }

        private void btnFreeRaces_Click(object sender, RoutedEventArgs e)
        {
            //@TODO: Improve this
            Application.Current.Properties["raceDashboardMode"] = "FreeRace";
            NavigateTo("UI/Pages/RaceDashboard.xaml");
        }

        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo("UI/Pages/TrackSelect.xaml");
        }

        private void btnTrackConfig_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo("UI/Pages/TrackConfig.xaml");
        }

        private void btnRankings_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo("UI/Pages/Rankings.xaml");
        }

        private void btnChampionships_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo("UI/Pages/ChampionshipList.xaml");
        }

        private void btnPreferences_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo("UI/Pages/Preferences.xaml");
        }

        private void OnConnectionStatusChanged(TcpClient sender, ConnectionStatusChangedEventArgs e)
        {
            //This is needed for UI changes from different thread
            Dispatcher.Invoke(new Action(() => {
                //Toggle buttons isEnabled
                SetConnected(e.Status);

                //Update the connection text
                txtTrackConnectionStatus.Text = String.Format("Pista: {0}\nStatus: {1}", "XXX", e.Status);
            }));
        }

        private void SetConnected(TcpClient.ConnectionStatus status)
        {
            bool isConnected = (status == TcpClient.ConnectionStatus.Connected);

            btnTrackConfig.IsEnabled = isConnected;
            btnFreeRaces.IsEnabled = isConnected;
            btnChampionships.IsEnabled = isConnected;

            btnConnect.Visibility = (isConnected ? Visibility.Hidden : Visibility.Visible);
            btnDisconnect.Visibility = (isConnected ? Visibility.Visible : Visibility.Hidden);
        }

        private void btnDisconnect_Click(object sender, RoutedEventArgs e)
        {
            SockectClient.Disconnect();
        }
    }
}
